package com.briup.corejava.day16;

import com.briup.corejava.day11.pojo.Student;

/**
 * 同步方法。即：将可能出现线程安全的代码封装到方法内部
 * 使用synchronized关键字修饰
 * 成员方法：默认锁对象为this
 * 静态方法：默认锁对象为类名.class
 */
public class Test3 {
    public static void main(String[] args){
        // 创建对象
        Printer printer = new Printer();
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    printer.print1();
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    printer.print2();
                }
            }
        };
        t1.start();
        t2.start();
    }
}
class Printer{
    public static synchronized void print1(){
        System.out.print("天");
        System.out.print("天");
        System.out.print("向");
        System.out.println("上");
    }
    public void print2(){
        synchronized (Printer.class){
            System.out.print("好");
            System.out.print("好");
            System.out.print("学");
            System.out.println("习");
        }
    }
}
